package model

import (
	"encoding/json"
)

type SimCardCreateRequest struct {
	Id            int64           `json:"id"`
	UserId        int64           `json:"user_id"`
	UserName      string          `json:"user_name"`
	UserPhone     string          `json:"user_phone"`
	AgentId       int64           `json:"agent_id"`
	AgentDetail   json.RawMessage `json:"agent_detail"`
	EbikeId       int64           `json:"ebike_id"`
	Sn            string          `json:"sn"`
	FrameCode     string          `json:"frame_code"`
	Iccid         string          `json:"iccid"`
	EffectiveDate string          `json:"effective_date"`
	ExpiredDate   string          `json:"expired_date"`
	Status        int             `json:"status"`
	Detail        json.RawMessage `json:"detail"`
	CreatedAt     int64           `json:"created_at"`
	UpdatedAt     int64           `json:"updated_at"`
}

type SimCardUpdateRequest struct {
	Id            *int64          `json:"id"`
	UserId        *int64          `json:"user_id"`
	EbikeId       *int64          `json:"ebike_id"`
	UserPhone     *string         `json:"user_phone"`
	AgentId       *int64          `json:"agent_id"`
	Sn            *string         `json:"sn"`
	FrameCode     *string         `json:"frame_code"`
	Iccid         *string         `json:"iccid"`
	EffectiveDate *string         `json:"effective_date"`
	ExpiredDate   *string         `json:"expired_date"`
	Status        *int            `json:"status"`
	Detail        json.RawMessage `json:"detail"`
	CreatedAt     *int64          `json:"created_at"`
	UpdatedAt     *int64          `json:"updated_at"`
}
type SimCardListRequest struct {
	Id               *int64          `json:"id"`
	Ids              []int64         `json:"ids"`
	EbikeId          *int64          `json:"ebike_id"`
	EbikeIds         []int64         `json:"ebike_ids"`
	UserId           *int64          `json:"user_id"`
	UserPhone        *string         `json:"user_phone"`
	AgentId          *int64          `json:"agent_id"`
	Sn               *string         `json:"sn"`
	Iccid            *string         `json:"iccid"`
	FrameCode        *string         `json:"frame_code"`
	EffectiveDate    *string         `json:"effective_date"`
	ExpiredDate      *int64          `json:"expired_date"`
	ExpiredDateRange []int64         `json:"expired_date_range"`
	Expiring         *bool           `json:"expiring"`
	Status           *int            `json:"status"`
	Detail           json.RawMessage `json:"detail"`
	CreatedAt        *int64          `json:"created_at"`
	UpdatedAt        *int64          `json:"updated_at"`
	Index            int             `json:"index"`
	Size             int             `json:"size"`
}
type SimCardListResponse struct {
	Total int            `json:"total"`
	List  []*SimCardInfo `json:"list"`
}
type SimCardInfoRequest struct {
	Id int64 `json:"id"`
}
type SimCardInfo struct {
	Id            int64           `json:"id"`
	UserId        int64           `json:"user_id"`
	UserPhone     string          `json:"user_phone"`
	AgentId       int64           `json:"agent_id"`
	EbikeId       int64           `json:"ebike_id"`
	AgentName     string          `json:"agent_name"`
	Sn            string          `json:"sn"`
	Iccid         string          `json:"iccid"`
	FrameCode     string          `json:"frame_code"`
	EffectiveDate string          `json:"effective_date"`
	ExpiredDate   string          `json:"expired_date"`
	Expiring      bool            `json:"expiring"`
	Status        int             `json:"status"`
	Detail        json.RawMessage `json:"detail"`
	CreatedAt     int64           `json:"created_at"`
	UpdatedAt     int64           `json:"updated_at"`
}
type SimCardDeleteRequest struct {
	Id       *int64  `json:"id"`
	Ids      []int64 `json:"ids"`
	EbikeId  *int64  `json:"ebike_id"`
	EbikeIds []int64 `json:"ebike_ids"`
}

type SimCardSyncRequest struct {
	EbikeId  int64   `json:"ebike_id"`
	EbikeIds []int64 `json:"ebike_id"`
	//Sn      string `json:"sn"`
}

type UpdateSimCardByConditionRequest struct {
	Id       *int64  `json:"id"`
	Ids      []int64 `json:"ids"`
	EbikeId  *int64  `json:"ebike_id"`
	EbikeIds []int64 `json:"ebike_ids"`
}

type SimCardUpsertRequest struct {
	EbikeId   int64   `json:"ebike_id"`
	UserId    *int64  `json:"user_id"`
	UserPhone *string `json:"user_phone"`
	AgentId   *int64  `json:"agent_id"`
	Sn        *string `json:"sn"`
	FrameCode *string `json:"frame_code"`
	Iccid     *string `json:"iccid"`
}

func (s *SimCardUpsertRequest) ToSimCardCreateRequest() *SimCardCreateRequest {

	ret := &SimCardCreateRequest{
		EbikeId: s.EbikeId,
	}

	if s.UserId != nil {
		ret.UserId = *s.UserId
	}
	if s.UserPhone != nil {
		ret.UserPhone = *s.UserPhone
	}
	if s.AgentId != nil {
		ret.AgentId = *s.AgentId
	}
	if s.Sn != nil {
		ret.Sn = *s.Sn
	}
	if s.FrameCode != nil {
		ret.FrameCode = *s.FrameCode
	}
	if s.Iccid != nil {
		ret.Iccid = *s.Iccid
	}
	return ret
}

type RechargeRequest struct {
	Iccid string `json:"iccid"`
}

type PreRechargeRequest struct {
	Mode  string             `json:"mode" binding:"required,oneof=async sync"`
	Items []*PreRechargeItem `json:"items"`
}

type PreRechargeItem struct {
	PrepaidId *int64 `json:"prepaid_id"`
	EbikeId   *int64 `json:"ebike_id"`
	SimCardId *int64 `json:"sim_card_id"`
}

type GetSimCardServiceDetailRequest struct {
	EBikeId int64 `json:"ebike_id" binding:"required"`
}

type GetSimCardServiceDetailResponse struct {
	EBikeId   int64      `json:"ebike_id"`   // 电车ID
	Status    CardStatus `json:"status"`     // 服务状态
	StartDate string     `json:"start_date"` // 服务开始日期
	EndDate   string     `json:"end_date"`   // 服务结束日期
}

type SimCardSyncStrategyType string

const (
	SimCardSyncFullForce SimCardSyncStrategyType = "Full Synchronization By Force"
	SimCardSyncFullMerge SimCardSyncStrategyType = "Full Synchronization By Merge"
)

type SimCardSyncDataType int

const (
	SimCardSyncDataAll SimCardSyncDataType = iota + 1
	SimCardSyncDataPartOf
)

type SimCardBaseInfoCronRequest struct {
	StrategyType SimCardSyncStrategyType
	DataMode     SimCardSyncDataType
	Ebikes       []int64 `json:"ebikes"`
}

type DataFetchType int

const (
	DataFetchTypePull DataFetchType = iota + 1
	DataFetchTypePush
)

type SyncRequest struct {
	Data      []*SimCardUpsertRequest `json:"data"`
	DataType  DataFetchType           `json:"data_fetch_type"`
	DataRange SimCardSyncDataType     `json:"data_range"`
}

type SimCardConsumptionInfoCronRequest struct {
	StrategyType SimCardSyncStrategyType
	DataMode     SimCardSyncDataType
	Ebikes       []int64 `json:"ebikes"`
	Id           *int64  `json:"id"`
	Ids          []int64 `json:"ids"`
}

type SimCardUpdateExpiringRequest struct {
	DataRange SimCardSyncDataType     `json:"data_range"`
	Data      []*SimCardUpsertRequest `json:"data"`
}

type UpdateActiveRequest struct {
	Data      []*SimCardUpsertRequest `json:"data"`
	DataRange SimCardSyncDataType     `json:"data_range"`
}

const (
	PendingInActiveTolerateDays = 45
)
